package sk.fiit.vppj.eventmanager.gui.administrator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;



import sk.fiit.vppj.eventmanager.core.DatabaseManager;
import sk.fiit.vppj.eventmanager.core.UserManager;
import sk.fiit.vppj.eventmanager.core.Utils;
import sk.fiit.vppj.eventmanager.data.BasicUser;
/**
 * Table model which manipulates with table in administrator's general view tab
 * @author Marek
 *
 */
final class BasicUserTableModel extends DefaultTableModel {
	private String[] columnNames = { "ID", "Name", "Usertype", "Class" };
	private List<BasicUser> data = null;
	private static final int PAGINATION = 25;

	public BasicUserTableModel() {
	}

	@Override
	public int getRowCount() {
		if( this.data == null ) {
			return 0;
		}
		return this.data.size();
	}

	@Override
	public int getColumnCount() {
		return this.columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return this.columnNames[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt( 0, columnIndex ).getClass();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if( columnIndex == 0 ) {
			return data.get( rowIndex ).getId();
		}
		else if( columnIndex == 1 ) {
			return data.get( rowIndex ).getName();
		}
		else if( columnIndex == 2 ) {
			return data.get( rowIndex ).getUsertype();
		}
		else if( columnIndex == 3 ) {
			return data.get( rowIndex ).getClazz();
		}
		else {
			return data.get( rowIndex ).getId();
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addTableModelListener(TableModelListener l) {

	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub

	}

	public int getPagination() {
		return PAGINATION;
	}


	private String getExtraSearchOpt(String exp) {
		String extSearch = null;
		if( exp.length() == 0 ) {
			extSearch = "";
		}
		else if( Utils.isNumber( exp ) ) {
			extSearch = "AND jos_users.id = ? ";
		}
		else {
			extSearch = "AND jos_users.name LIKE CONCAT('%',?,'%') ";
		}
		return extSearch;
	}

	private PreparedStatementCreator prepareStatement(final String exp, final int page) {
		final String sql = "SELECT jos_users.id, jos_users.name, jos_users.usertype, jos_event_manager_users.class, jos_event_manager_users.group_id, "
				+ " jos_event_manager_users.event_id FROM jos_users JOIN jos_event_manager_users ON "
				+ " jos_users.id = jos_event_manager_users.user_id WHERE jos_event_manager_users.event_id = ? " + getExtraSearchOpt( exp )
				+ " ORDER BY jos_users.name LIMIT ?, ?";

		return new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement( sql );
				ps.setInt( 1, UserManager.getInstance().getUser().getEvent_id() );
				if( exp.length() == 0 ) {
					ps.setInt( 2, page * PAGINATION );
					ps.setInt( 3, PAGINATION );
				}
				else if( Utils.isNumber( exp ) ) {
					ps.setInt( 2, Integer.parseInt( exp ) );
					ps.setInt( 3, page * PAGINATION );
					ps.setInt( 4, PAGINATION );
				}
				else {
					ps.setString( 2, exp );
					ps.setInt( 3, page * PAGINATION );
					ps.setInt( 4, PAGINATION );
				}
				return ps;
			}
		};
	}

	public void prepareData(final String exp, final int page) {

		this.data = DatabaseManager.getInstance().getJdbcTemplate().query( prepareStatement( exp, page ), new RowMapper<BasicUser>() {

			public BasicUser mapRow(ResultSet rs, int rowNum) throws SQLException {
				BasicUser tempUser = new BasicUser();
				tempUser.setId( rs.getInt( "id" ) );
				tempUser.setName( rs.getString( "name" ) );
				tempUser.setUsertype( rs.getString( "usertype" ) );
				tempUser.setClazz( rs.getString( "class" ) );
				tempUser.setEvent_id( rs.getInt( "event_id" ) );
				tempUser.setEvent_id( rs.getInt( "group_id" ) );

				return tempUser;
			}

		} );
	}

}
